package com.eqdd.yiqidian.ui.discover.workcircle;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CommentResultBean;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.MsgListBean;
import com.eqdd.yiqidian.bean.rebuild.MsgListBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.InputDialogActivity;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-1-24  下午1:09.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */
@DealCode(codes = {CodeRule.WORKCIRCLE_MSG})
public class MsgListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private int pageNum;
    private String talkTo;
    private int id;

    @Subscribe(codes = {CodeRule.WORKCIRCLE_MSG}, threadMode = ThreadMode.MAIN)
    public void refreshNews() {

    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "消息");

    }

    @Override
    public void initData() {
        isDealCode = true;
        isRxbus = true;
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SlimAdapterEx slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_104, new SlimInjector<MsgListBean>() {
            @Override
            public void onInject(MsgListBean data, IViewInjector injector) {
                injector.text(R.id.tv_name, data.getStaffName())
                        .text(R.id.tv_time, data.getCreateTime())
                        .image(R.id.iv_head, data.getIphoto())
                        .text(R.id.tv_reply, data.getCommentContent())
                        .text(R.id.tv_content, data.getCommentContent())
                        .visibility(R.id.iv_link, data.getWorkCircleImageUrl() == null ? View.INVISIBLE : View.VISIBLE)
                        .image(R.id.iv_link, data.getWorkCircleImageUrl())
                        .clicked(R.id.ll_content, v -> {
                            Bundle bundle = new Bundle();
                            bundle.putInt(Config.ID,data.getWorkCircleId());
                            startActivity(WorkCircleInfoActivity.class,bundle);
                        });
//                        .clicked(R.id.tv_reply_to,v -> {
//                            id =data.getWorkCircleId();
//                            talkTo =data.getStaffName();
//                            talkTo =data.getId();
//                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<MsgListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResultRows<MsgListBean>>post(HttpConfig.BASE_URL + HttpConfig.WORKCIRCLE_MSG)
                        .params("page", pageNum)
                        .params("userGuid", AppConstent.user.getGuid())
                        .converter(new JsonConverter<HttpResultRows<MsgListBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getItems().getPage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<MsgListBean>() : listResultBean.getItems().getRows());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });

            }
        });
        MVCCoolHelper<List<MsgListBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }
//    private void talkTo() {
//        Bundle bundle = new Bundle();
//        bundle.putString(Config.TALK_TO, talkTo);
//        startActivityForResult(InputDialogActivity.class, bundle, Config.INPUT);
//    }
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == Config.INPUT && resultCode == Config.SUCCESS) {
//            String content = data.getStringExtra(Config.CONTENT);
//            HttpUtil.commentWorkCircle(this, id, parentId, content, parentUserGuid, firstCommentId, (commentBean) -> {
//                if (slimTalkAdapter == null) {
//                    slimAdapterEx.getData().add(0, (CommentResultBean) commentBean);
//                    slimAdapterEx.notifyItemInserted(0);
//                } else if (isCommentUp) {
//                    slimTalkAdapter.getData().add(0, (CommentResultBean) commentBean);
//                    slimTalkAdapter.notifyItemInserted(0);
//                } else {
//                    slimTalkAdapter.getData().add((CommentResultBean) commentBean);
//                    slimTalkAdapter.notifyItemInserted(slimTalkAdapter.getData().size() - 1);
//                }
//            });
//        }
//    }

    @Override
    public void setView() {

    }
}
